package com.zwx.bingo.abstraction.endpoint;

import com.zwx.bingo.abstraction.service.ConditionalService;
import com.zwx.bingo.data.body.AbstractQueryRequestBody;
import com.zwx.bingo.loggable.annotation.Loggable;
import com.zwx.bingo.stateful.Responses;
import com.zwx.bingo.stateful.body.StatefulBody;
import com.zwx.bingo.stateful.body.WithResultBody;
import com.zwx.bingo.stateful.body.impl.SuccessResponseBody;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.Serializable;

@FunctionalInterface
public interface DeleteByConditionEndpoint<ENTITY extends Serializable, QUERY extends AbstractQueryRequestBody> {

    @Autowired
    public abstract ConditionalService<ENTITY, QUERY> getConditionalService();

    //TODO 添加权限点控制
    @Loggable
    @DeleteMapping(value = "/delete", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    @ApiOperation(value = "Delete by conditions")
    public default ResponseEntity<StatefulBody<ENTITY>> delete(
            @RequestBody QUERY requestBody) {
        WithResultBody<Integer> withResultBody = new WithResultBody<>();
        withResultBody.setResult(getConditionalService().deleteByCondition(requestBody));
        return Responses.ok(SuccessResponseBody.builder().payload(withResultBody).build());
    }
}
